package com.dingquan.springbootdingquan.services;

import com.dingquan.springbootdingquan.dao.UserRepository;
import com.dingquan.springbootdingquan.pojo.Dto.UserDto;
import com.dingquan.springbootdingquan.pojo.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServices implements IServices {


    @Autowired
    UserRepository userRepository;


    @Override
    public User add(UserDto user) {

        User userPojo = new User();

        BeanUtils.copyProperties(user,userPojo);
        return userRepository.save(userPojo);
    }

    @Override
    public User update(UserDto user) {

        User userPojo = new User();
        BeanUtils.copyProperties(user,userPojo);
        return userRepository.save(userPojo);
    }


    @Override
    public User select(Integer userId) {
        return userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("用户不存在,请检查重试!"));
    }

    @Override
    public void delete(Integer userId) {
        userRepository.deleteById(userId);
    }
}
